
package com.wangzha.business.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.wangzha.common.base.service.BaseService;
import com.wangzha.common.kit.SqlKit;
import com.wangzha.common.model.UserUploadFile;
import com.wangzha.common.vo.Grid;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 
 * @author zhb
 * @date 2021-02-05
 */
public class UserUploadFileService extends BaseService {
    private UserUploadFile dao = new UserUploadFile().dao();


    @Override
    public Model<UserUploadFile> getDao() {
        return dao;
    }


    public boolean saveUserUploadFile(List<UserUploadFile> userUploadFileList){
        Db.batchSave(userUploadFileList, userUploadFileList.size());
        return true;
    }

    public Grid queryListNotInIds(int pageNumber, int pageSize, List<Long> ids, String userCode, String name) {
        String sql = "select * from " + getTable() + " where user_code = '" + userCode + "'";
        if (CollectionUtils.isNotEmpty(ids)) {
            sql += " and id not in  " + SqlKit.joinId(ids);
        }
        if (StringUtils.isNotEmpty(name)) {
            sql += " and name like '%" + name +"%'";
        }

        return queryForList(sql, pageNumber, pageSize, null," order by id");
    }

    public Grid queryListInIds(int pageNumber, int pageSize, List<Long> ids, String userCode, String name) {
        if (CollectionUtils.isEmpty(ids)) {
            return new Grid();
        }
        StringBuffer sbf = new StringBuffer();
        for (Long id : ids) {
            if (sbf.length() > 0) {
                sbf.append(",");
            }
            sbf.append("'").append(id).append("'");
        }
        String sql = "select * from " + getTable() + " where user_code = '" + userCode +"' ";
        sql += " and id in (" + sbf.toString() + ") order by field(id, " + sbf.toString() + ")";
        if (StringUtils.isNotEmpty(name)) {
            sql += " and name like '%" + name +"%'";
        }

        return queryForList(sql, pageNumber, pageSize, null,null);
    }


    public Grid queryByIds(int pageNumber, int pageSize, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new Grid();
        }
        StringBuffer sbf = new StringBuffer();
        for (String id : ids) {
            if (sbf.length() > 0) {
                sbf.append(",");
            }
            sbf.append("'").append(id).append("'");
        }
        String sql = "select * from " + getTable() + " where id in (" + sbf.toString() + ") order by field(id, " + sbf.toString() + ")";
        return queryForList(sql, pageNumber, pageSize, null,null);
    }

    public List<UserUploadFile> findByRecordIds(List<Long> recordIds) {
        if (CollectionUtils.isEmpty(recordIds)) {
            return null;
        } else {
            StringBuffer sbf = new StringBuffer();
            for (Long id : recordIds) {
                if (sbf.length() > 0) {
                    sbf.append(",");
                }
                sbf.append("'").append(id).append("'");
            }
            String sql = "select * from " + getTable() + " where id in (" + sbf.toString() + ") order by field(id, " + sbf.toString() + ")"; ;
            return getDao().find(sql);
        }
    }

}

